package com.pan.alg.niuke.link;


public class MergeTwoLinkList {

    public static ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null&&list2==null){
            return null;
        }else if(list1==null){
            return list2;
        }else if(list2==null){
            return list1;
        }

        ListNode list3Current = null;
        ListNode list3Head = null;
        ListNode list1Current = list1;
        ListNode list2Current = list2;
        if(list1.val<=list2.val){
            list3Head = list1;
            list1Current = list1.next;
            list3Current=list1;
        }else{
            list3Head = list2;
            list2Current=list2.next;
            list3Current=list2;
        }

        while(list1Current!=null||list2Current!=null){

            if(list1Current==null||(list2Current!=null&&list1Current.val>list2Current.val)){
                list3Current.next=list2Current;
                list2Current=list2Current.next;
                list3Current=list3Current.next;

            }else if(list2Current==null||(list1Current!=null&&list1Current.val<=list2Current.val)){
                list3Current.next=list1Current;
                list1Current=list1Current.next;
                list3Current=list3Current.next;
            }
            list3Current.next=null;
        }
        return list3Head;
    }

    public static void main(String[] args) {
        int[] arr1 ={1,16,17};
        int[] arr2 ={16,16,16};
        ListNode listNode1 = ListNodeUtil.transArr2List(arr1);
        ListNode listNode2 = ListNodeUtil.transArr2List(arr2);
        ListNode merge = Merge(listNode1, listNode2);
        ListNodeUtil.printListNode(merge);
    }



}

